package com.da.javatest.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

/**
 * @author chenlida
 * @date 2019/8/31 11:02
 * @description
 */
public class FileReadUtil {

    /**
     * 以行为单位读取文件，读取到最后一行
     *
     * @param filePath
     * @return
     */
    public static List<String> readFileByPath(String filePath) {
        try {
            List<String> listContent = FileUtils.readLines(new File(filePath), "UTF-8");
            return listContent;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static List<String> readFileByName(String fileName) {
        return readFileByPath(getProjectRootPath() + fileName);
    }

    private static String getProjectRootPath() {
        return Thread.currentThread().getContextClassLoader().getResource("data").getPath() + "/";
    }

    public static void main(String[] args) {
        List<String> list = readFileByName("category.txt");
        System.out.println(list.size());
        Map<String, Long> categoryMap = new HashMap<>(list.size());
        for (String data : list) {
            String[] split = data.split("_", 2);
            categoryMap.put(split[1], Long.parseLong(split[0]));
        }
        System.out.println(categoryMap.size());
        System.out.println("-----------------------------------");
        System.out.println(categoryMap.get("1_Jewelry & Accessories"));
    }
}